<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="学校名称" prop="schoolName">
        <el-input
          v-model="queryParams.schoolName"
          placeholder="请输入学校名称"
          clearable
          @keyup.enter="handleQuery"
        />

      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            @change="handleQuery"
            style="width:200px;"
        >
          <el-option label="活跃" value="活跃"></el-option>
          <el-option label="不活跃" value="不活跃"></el-option>
          <el-option label="待审核" value="待审核"></el-option>
          <el-option label="已拒绝" value="已拒绝"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加盟日期" prop="joinDate">
        <el-date-picker clearable
          v-model="queryParams.joinDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择加盟日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后更新时间" prop="lastUpdated">
        <el-date-picker clearable
          v-model="queryParams.lastUpdated"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['platform:schools:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['platform:schools:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['platform:schools:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['platform:schools:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schoolsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学校ID" align="center" prop="schoolId" />
      <el-table-column label="学校名称" align="center" prop="schoolName" />
      <el-table-column label="学校地址" align="center" prop="address" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="加盟日期" align="center" prop="joinDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.joinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学校状态" align="center" prop="status" />
      <el-table-column label="最后更新时间" align="center" prop="lastUpdated" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastUpdated, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['platform:schools:edit']">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学校信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="schoolsRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="form.schoolName" placeholder="请输入学校名称" />
        </el-form-item>
        <el-form-item label="学校地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="加盟日期" prop="joinDate">
          <el-date-picker clearable
            v-model="form.joinDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择加盟日期">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="状态" prop="status">
          <el-select
              v-model="form.status"
              placeholder="请选择状态"
              clearable
              style="width:200px;"
          >
            <el-option label="批准" value="活跃"></el-option>
            <el-option label="拒绝" value="已拒绝"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="最后更新时间" prop="lastUpdated">
          <el-date-picker clearable
            v-model="form.lastUpdated"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Schools">
import { listSchools, getSchools, delSchools, addSchools, updateSchools } from "@/api/platform/schools"

const { proxy } = getCurrentInstance()

const schoolsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
import { useExampleStore } from "@/stores/exampleStore"; // 引入 Pinia store
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    schoolName: null,
    address: null,
    contactPerson: null,
    contactPhone: null,
    email: null,
    joinDate: null,
    status: null,
    lastUpdated: null
  },
  rules: {
    schoolName: [
      { required: true, message: "学校名称不能为空", trigger: "blur" }
    ],
    joinDate: [
      { required: true, message: "加盟日期不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "学校状态: 活跃, 不活跃, 待审核, 已拒绝不能为空", trigger: "change" }
    ],
    lastUpdated: [
      { required: true, message: "最后更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)
/** 查询学校信息管理列表 */
function getList() {
  loading.value = true
  listSchools(queryParams.value).then(response => {
    schoolsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    schoolId: null,
    schoolName: null,
    address: null,
    contactPerson: null,
    contactPhone: null,
    email: null,
    joinDate: null,
    status: null,
    lastUpdated: null
  }
  proxy.resetForm("schoolsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.schoolId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加学校信息管理"
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _schoolId = row.schoolId || ids.value
  getSchools(_schoolId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改学校信息管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["schoolsRef"].validate(valid => {
    if (valid) {
      if (form.value.schoolId != null) {
        updateSchools(form.value).then(response => {
          proxy.$modal.msgSuccess("审核成功")
          open.value = false
          getList()
        })
      } else {
        addSchools(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _schoolIds = row.schoolId || ids.value
  proxy.$modal.confirm('是否确认删除学校信息管理编号为"' + _schoolIds + '"的数据项？').then(function() {
    return delSchools(_schoolIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('platform/schools/export', {
    ...queryParams.value
  }, `schools_${new Date().getTime()}.xlsx`)
}

getList()
</script>
